<?php
require_once(dirname(__FILE__).'/../../../../inc/class/init.php');
require_once(dirname(__FILE__)."/../../../../inc/session_auth.php");
	if(!class_exists('WFWidgetPoolOpt')){
		require_once(dirname(__FILE__).'/../../../../inc/class/widgetPool/config/WidgetPoolOpt.php');
	}
	try{
		$arrAnalyzeResult = array();
	    //WF::getWidgetFactory()->getInterface()->initWFDBSchema();
	    //Step 1: Get WGF Version
	    $strVersion = WF::getConfigFactory()->getConfig()->getVersion();
	    $strPathWidgetsNew = WF::getDocumentRoot().'/repository/widgets_new';
	    $isExistsWidgetsNew = WF::getIOFactory()->getFile()->isFileExists($strPathWidgetsNew);
	    $date = date('Y-m-d H:i:s');
	    $strAnalyzeResultHeader = '
>>>> Start Anaylze WGF : '.$date.'<br />';
$strAnalyzeResultHeader .= analyzeWFShowItemInfo('Current PHP version: '.phpversion());
$strAnalyzeResultHeader .= analyzeWFShowItemInfo('PHP extensions: '.implode(', ',get_loaded_extensions()));
$strAnalyzeResultHeader .= analyzeWFShowItemInfo('WGF version : '.$strVersion);
$strAnalyzeResultHeader .= analyzeWFShowItemInfo('WGF current wp in /path/to/widgetPool/config.php : '.basename(WF::getWidgetPoolFactory()->getWPHome()));
if($isExistsWidgetsNew){
	$strAnalyzeResultHeader .= analyzeWFShowItemInfo('WGF is /path/to/widgets_new exists : true');
}

$strPathUpdateWidgetPidFile = WF::getDocumentRoot() . '/repository/widgetpool.pid';
if(true === WF::getIOFactory()->getFile()->isFileExists($strPathUpdateWidgetPidFile)){
	$strAnalyzeResultHeader .= analyzeWFShowItemInfo('WGF '.$strPathUpdateWidgetPidFile.' exists : true');
}
$strPathAnalyzeWidgetsNewInfoPidFile = WF::getDocumentRoot() . '/repository/analyzeWidgetsNewInfo.pid';
if(true === WF::getIOFactory()->getFile()->isFileExists($strPathAnalyzeWidgetsNewInfoPidFile)){
	$strAnalyzeResultHeader .= analyzeWFShowItemInfo('WGF '.$strPathAnalyzeWidgetsNewInfoPidFile.' exists : true');
}
	    // WGF /path/to/repository read/write permissions check 
	    $strPathRepository = WF::getDocumentRoot().'/repository/';
	    try{
	    	WF::getIOFactory()->getFile()->isReadableWritableRecursive($strPathRepository);
	    }catch(Exception $e){
	    	$strUrlWGFBase = str_replace('/repository/widgetPool/'.basename(WF::getWidgetPoolFactory()->getWPHome()).'/interface/analyzeWF.php','',$_SERVER['PHP_SELF']);
	    	$strAnalyzeResultHeader .= analyzeWFShowItemError('WGF /path/to/repository check read/write permissions : '.analyzeWFShowPoint('failed').'<br />Error Msg : '.$e->getMessage().'<br />To solved this problem please reference document <a href="'.$strUrlWGFBase.'/common/tool/diagnostic/doc/howTo.txt" target="_blank">here</a>.');
	    } 
	    
	    $strPathTemp = $_SERVER['TMP'];
	    if(WF::getIOFactory()->getFile()->isFileExists($strPathTemp) && false === WF::getIOFactory()->getFile()->isReadableWritable($strPathTemp)){
	    	$strUrlWGFBase = str_replace('/repository/widgetPool/'.basename(WF::getWidgetPoolFactory()->getWPHome()).'/interface/analyzeWF.php','',$_SERVER['PHP_SELF']);
	    	$strAnalyzeResultHeader .= analyzeWFShowItemError($strPathTemp.' check read/write permissions : '.analyzeWFShowPoint('failed').'<br />To solved this problem please reference document <a href="'.$strUrlWGFBase.'/common/tool/diagnostic/doc/howTo.txt" target="_blank">here</a>.');
	    }
	    // check widgets_new
	    if($isExistsWidgetsNew){
	    	// Check is /path/to/widgets_new/widget/* folder contains widget.xml
		    $arrDirTmp1 = glob(WF::getDocumentRoot() . '/repository/widgets_new/widget/*', GLOB_ONLYDIR);
	        //$arrDirTmp2 = glob(WF::getDocumentRoot() . '/repository/userGenerated/widget/*', GLOB_ONLYDIR);
		    $arrDirTmp2 = array();
	        $arrDir = array();
	        if((is_array($arrDirTmp1) && $arrDirTmp1) || (is_array($arrDirTmp2) && $arrDirTmp2)){
	            $arrDir = array_merge($arrDirTmp1, $arrDirTmp2);
	            // Check widget.xml is exists or not
	            // Check widget.xml is well-formed XML or not
	            foreach($arrDir as $strFullPathDir){
	                // get modA from /path/to/modA
	                $strWidgetName = basename($strFullPathDir);
	                //echo 'strDirName: ' . $strWidgetName . '<br />';
	                //$this->synWidgetDataToDBByWidgetName($strWidgetName);
	                $strPathWidgetXml = $strFullPathDir.'/widget.xml';
	                if(false === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetXml)){
	                	$strAnalyzeResult .= analyzeWFShowItemError('WGF lack of file /path/to/repository/widgets_new/widget/'.$strWidgetName.'/widget.xml');
	                }else{
		                if(false === simplexml_load_file($strPathWidgetXml)){
		                	$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/repository/widgets_new/widget/'.$strWidgetName.'/widget.xml is not a well-formed XML');
		                }
	                }
	            }
	        }
	        
	        // Check is /path/to/widgets_new/widgetComponent/* folder contains component.xml
	        $arrDirTmp1 = glob(WF::getDocumentRoot() . '/repository/widgets_new/widgetComponent/*', GLOB_ONLYDIR);
	        $arrDirTmp2 = array();
	        $arrDir = array();
	        if((is_array($arrDirTmp1) && $arrDirTmp1) || (is_array($arrDirTmp2) && $arrDirTmp2)){
	        	$arrDir = array_merge($arrDirTmp1, $arrDirTmp2);
	        	// Check component.xml is exists or not
	        	// Check component.xml is well-formed XML or not
	        	foreach($arrDir as $strFullPathDir){
	        		// get modA from /path/to/modA
	        		$strWidgetComponentName = basename($strFullPathDir);
	        		$strPathWidgetComponentXml = $strFullPathDir.'/component.xml';
	        		if(false === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetComponentXml)){
	        			$strAnalyzeResult .= analyzeWFShowItemError('WGF lack of file /path/to/repository/widgets_new/widgetComponent/'.$strWidgetComponentName.'/component.xml');
	        		}else{
		        		if(false === simplexml_load_file($strPathWidgetComponentXml)){
		        			$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/repository/widgets_new/widgetComponent/'.$strWidgetComponentName.'/component.xml is not a well-formed XML');
		        		}
	        		}
	        	}
	        }
	        
	        // Get widgets_new_info.xml
	        $strPathWidgetsNewInfoXml = WF::getDocumentRoot().'/repository/widgets_new_info.xml';
	        $isExistsWidgetsNewInfoXml = false; //default
	        if(true === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetsNewInfoXml)){
	        	$isExistsWidgetsNewInfoXml = true;
	        	if(false === simplexml_load_file($strPathWidgetsNewInfoXml)){
	        		$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/repository/widgets_new_info.xml is not a well-formed XML');
	        	}
	        	$strAnalyzeResult .= analyzeWFShowItemInfo('Details content of '.$strPathWidgetsNewInfoXml.'<br />'.analyzeWFShowCode(htmlentities(file_get_contents($strPathWidgetsNewInfoXml))));
	        }
	        
	        $objWidgetsNewInfoXmlManager = WF::getWidgetPoolFactory()->getWidgetsNewInfoXmlManager();
	        $objWidgetInfoXmlManager = WF::getWidgetPoolFactory()->getWidgetInfoXmlManager();
	        $objWidgetComponentInfoXmlManager = WF::getWidgetPoolFactory()->getWidgetComponentInfoXmlManager();
	        // Get /path/to/widgets_new/widget/widget_info.xml
	        $strPathWidgetsNewWidgetInfoXml = WF::getDocumentRoot().'/repository/widgets_new/widget/widget_info.xml';
	        $strPathWpCurrentWidgetInfoXml = WF::getWidgetPoolFactory()->getWPHome().'/widget/widget_info.xml';
	        if(false === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetsNewWidgetInfoXml)){
	        	$strAnalyzeResult .= analyzeWFShowItemError('WGF lack of file /path/to/widgets_new/widget/widget_info.xml');
	        }else{
	        	if(false === simplexml_load_file($strPathWidgetsNewWidgetInfoXml)){
	        		$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/widgets_new/widget/widget_info.xml is not a well-formed XML');
	        	}
	        	$isShowXml = false; //default
	        	if(true === $isExistsWidgetsNewInfoXml){
	        		$intConfigureWidgetUpdateStatusFromWidgetsNewInfoXml = $objWidgetsNewInfoXmlManager->getConfigureWidgetUpdateStatusFromWidgetsNewInfoXml();
	        		if($intConfigureWidgetUpdateStatusFromWidgetsNewInfoXml == WFWidgetPoolOpt::WP_UPDATE_STATUS_NEED_UPDATE){
	        			$strConfigureWidgetVersionFromWidgetsNew = $objWidgetInfoXmlManager->getConfigureWidgetVersionFromWidgetsNew();
	        			$strConfigureWidgetVersionFromWpCurrent = $objWidgetInfoXmlManager->getConfigureWidgetVersionFromWpCurrent();
	        			if(version_compare($strConfigureWidgetVersionFromWidgetsNew, $strConfigureWidgetVersionFromWpCurrent, '<=')){
	        				$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/widgets_new/widget/widget_info.xml configure widget version['.$strConfigureWidgetVersionFromWidgetsNew.'] should greater than /path/to/widgetPool/wp[Current]/widget/widget_info.xml configure widget version['.$strConfigureWidgetVersionFromWpCurrent.']');
	        				$isShowXml = true;
	        			}
	        		}
	        	}
	        	if($isShowXml){
	        		$strAnalyzeResult .= analyzeWFShowItemInfo('Details content of '.$strPathWidgetsNewWidgetInfoXml.'<br />'.analyzeWFShowCode(htmlentities(file_get_contents($strPathWidgetsNewWidgetInfoXml))));
	        		$strAnalyzeResult .= analyzeWFShowItemInfo('Details content of '.$strPathWpCurrentWidgetInfoXml.'<br />'.analyzeWFShowCode(htmlentities(file_get_contents($strPathWpCurrentWidgetInfoXml))));
	        	}
	        }
	        // Get /path/to/widgets_new/widgetComponent/widget_component_info.xml
	        $strPathWidgetsNewWidgetComponentInfoXml = WF::getDocumentRoot().'/repository/widgets_new/widgetComponent/widget_component_info.xml';
	        $strPathWpCurrentWidgetComponentInfoXml = WF::getWidgetPoolFactory()->getWPHome().'/widgetComponent/widget_component_info.xml';
	        if(false === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetsNewWidgetComponentInfoXml)){
	        	$strAnalyzeResult .= analyzeWFShowItemError('WGF lack of file /path/to/widgets_new/widgetComponent/widget_component_info.xml');
	        }else{
	        	if(false === simplexml_load_file($strPathWidgetsNewWidgetComponentInfoXml)){
	        		$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/widgets_new/widgetComponent/widget_component_info.xml is not a well-formed XML');
	        	}
	        	$isShowXml = false; //default
	        	if(true === $isExistsWidgetsNewInfoXml){
	        		$arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml = $objWidgetsNewInfoXmlManager->getArrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml();
	        		if(is_array($arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml) && $arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml){
	        			foreach($arrsWidgetComponentUpdateStatusFromWidgetsNewInfoXml as $arrWidgetComponentUpdateStatusFromWidgetsNewInfoXml){
	        				if($arrWidgetComponentUpdateStatusFromWidgetsNewInfoXml['update_status'] == WFWidgetPoolOpt::WP_UPDATE_STATUS_NEED_UPDATE){
	        					$strProductId = $arrWidgetComponentUpdateStatusFromWidgetsNewInfoXml['@attributes']['id'];
	        					
	        					$assocProductIdWithWidgetComponentVersionFromWidgetsNew = $objWidgetComponentInfoXmlManager->getAssocProductIdWithWidgetVersionFromWidgetsNew();
	        					$assocProductIdWithWidgetComponentVersionFromWpCurrent = $objWidgetComponentInfoXmlManager->getAssocProductIdWithWidgetVersionFromWpCurrent();
	        					if(isset($assocProductIdWithWidgetComponentVersionFromWidgetsNew[$strProductId]) && isset($assocProductIdWithWidgetComponentVersionFromWpCurrent[$strProductId])){
	        						$strWidgetComponentVersionFromWidgetsNew = $assocProductIdWithWidgetComponentVersionFromWidgetsNew[$strProductId]['widget_component_version'];
	        						$strWidgetComponentVersionFromWpCurrent = $assocProductIdWithWidgetComponentVersionFromWpCurrent[$strProductId]['widget_component_version'];
	        						if(version_compare($strWidgetComponentVersionFromWidgetsNew, $strWidgetComponentVersionFromWpCurrent, '<=')){
	        							$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/widgets_new/widgetComponent/widget_component_info.xml widgetComponent of product_type['.$strProductId.'] version['.$strWidgetComponentVersionFromWidgetsNew.'] should greater than /path/to/widgetPool/wp[Current]/widgetComponent/widget_component_info.xml widgetComponent version['.$strWidgetComponentVersionFromWpCurrent.']');
	        							$isShowXml = true;
	        						}
	        					}
	        				}
	        			}
	        		}
	        	}
	        	if($isShowXml){
	        		$strAnalyzeResult .= analyzeWFShowItemInfo('Details content of '.$strPathWidgetsNewWidgetComponentInfoXml.'<br />'.analyzeWFShowCode(htmlentities(file_get_contents($strPathWidgetsNewWidgetComponentInfoXml))));
	        		$strAnalyzeResult .= analyzeWFShowItemInfo('Details content of '.$strPathWpCurrentWidgetComponentInfoXml.'<br />'.analyzeWFShowCode(htmlentities(file_get_contents($strPathWpCurrentWidgetComponentInfoXml))));
	        	}
	        }
	        //<fieldset><legend>widgets_new analyze result :</legend>'.$strAnalyzeResult.'</fieldset>';
	        $arrAnalyzeResult[] = '<fieldset><legend style="color: white;font-size: 110%;font-weight: bold;background-color: #999;padding: 2px;">WidgetPool => widgets_new analyze result :</legend>'.$strAnalyzeResult.'</fieldset>';
	        $strAnalyzeResult = ''; //clean
	    }
	    
	    // wpCurrent analyze
	    $strPathWpCurrent = WF::getWidgetPoolFactory()->getWPHome();
	    $isExistsWpCurrent = WF::getIOFactory()->getFile()->isFileExists($strPathWpCurrent);
	    if($isExistsWpCurrent){
	    	// Check is /path/to/repository/widgetPool/wp[Current]/widget/* folder contains widget.xml
	    	$arrDirTmp1 = glob($strPathWpCurrent.'/widget/*', GLOB_ONLYDIR);
	    	$arrDirTmp2 = glob(WF::getDocumentRoot() . '/repository/userGenerated/widget/*', GLOB_ONLYDIR);
	    	$arrDir = array();
	    	if((is_array($arrDirTmp1) && $arrDirTmp1) || (is_array($arrDirTmp2) && $arrDirTmp2)){
	    		$arrDir = array_merge($arrDirTmp1, $arrDirTmp2);
	    		// Check widget.xml is exists or not
	    		// Check widget.xml is well-formed XML or not
	    		foreach($arrDir as $strFullPathDir){
	    			// get modA from /path/to/modA
	    			$strWidgetName = basename($strFullPathDir);
	    			//echo 'strDirName: ' . $strWidgetName . '<br />';
	    			//$this->synWidgetDataToDBByWidgetName($strWidgetName);
	    			$strPathWidgetXml = $strFullPathDir.'/widget.xml';
	    			if(false === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetXml)){
	    				$strAnalyzeResult .= analyzeWFShowItemError('WGF lack of file '.$strPathWidgetXml);
	    			}else{
	    				if(false === simplexml_load_file($strPathWidgetXml)){
	    					$strAnalyzeResult .= analyzeWFShowItemError('WGF '.$strPathWidgetXml.' is not a well-formed XML');
	    				}
	    			}
	    		}
	    	}
	    	 
	    	// Check is /path/to/repository/widgetPool/wp[Current]/widgetComponent/* folder contains component.xml
	    	$arrDirTmp1 = glob($strPathWpCurrent.'/widgetComponent/*', GLOB_ONLYDIR);
	    	$arrDirTmp2 = array();
	    	$arrDir = array();
	    	if((is_array($arrDirTmp1) && $arrDirTmp1) || (is_array($arrDirTmp2) && $arrDirTmp2)){
	    		$arrDir = array_merge($arrDirTmp1, $arrDirTmp2);
	    		// Check component.xml is exists or not
	    		// Check component.xml is well-formed XML or not
	    		foreach($arrDir as $strFullPathDir){
	    			// get modA from /path/to/modA
	    			$strWidgetComponentName = basename($strFullPathDir);
	    			$strPathWidgetComponentXml = $strFullPathDir.'/component.xml';
	    			if(false === WF::getIOFactory()->getFile()->isFileExists($strPathWidgetComponentXml)){
	    				$strAnalyzeResult .= analyzeWFShowItemError('WGF lack of file /path/to/repository/widgetPool/wp[Current]/widgetComponent/'.$strWidgetComponentName.'/component.xml');
	    			}else{
	    				if(false === simplexml_load_file($strPathWidgetComponentXml)){
	    					$strAnalyzeResult .= analyzeWFShowItemError('WGF /path/to/repository/widgetPool/wp[Current]/widgetComponent/'.$strWidgetComponentName.'/component.xml is not a well-formed XML');
	    				}
	    			}
	    		}
	    	}
	    	if($strAnalyzeResult){ 
	    		$arrAnalyzeResult[] = '<fieldset><legend style="color: white;font-size: 110%;font-weight: bold;background-color: #999;padding: 2px;">WidgetPool => '.basename($strPathWpCurrent).' analyze result :</legend>'.$strAnalyzeResult.'</fieldset>';
	    	}
	    	$strAnalyzeResult = ''; //clean
	    }
	    $strAnalyzeResultFooter .= '
>>>> End Anaylze WGF : '.$date;
	    echo nl2br($strAnalyzeResultHeader.implode('<br />',$arrAnalyzeResult).$strAnalyzeResultFooter);
	} catch (Exception $e) {
	    $msg = $e->getMessage();
	    myerror_log($msg);
	}
function analyzeWFShowItem($str){
	return $str.'<hr />';
}
function analyzeWFShowItemInfo($str){
	return '<span style="color: #666;font-size: 80%;">[INFO]</span> '.$str.'<hr />';
}
function analyzeWFShowItemError($str){
	return '<span style="color: red;font-weight: bold;">[ERROR]</span> '.$str.'<hr />';
}
function analyzeWFShowCode($str){
	return '<span style="color: #666;font-size: 80%;">'.$str.'</span>';
}
function analyzeWFShowPoint($str){
	return '<span style="color: red;font-weight: bold;font-size: 120%;">'.$str.'</span>';
}